package FriendTable.Gui;
/*
更新A
 */

import FriendTable.Friend;
import FriendTable.FriendDAO;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SearchPanel extends JPanel {
    private InputPanel idInput;
    private JButton searchButton;
    private JTextArea resultArea;

    public SearchPanel() {
        setLayout(new GridLayout(4, 2));

        idInput = new InputPanel("ID:", null);
        add(idInput);

        searchButton = new JButton("查询");
        add(searchButton);

        resultArea = new JTextArea();
        add(resultArea);

        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int id;
                try {
                    id = Integer.parseInt(idInput.textField.getText());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(null, "请输入有效的ID！", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                Friend friend = FriendDAO.findFriendById(id);
                if (friend != null) {
                    resultArea.setText(friend.toString());
                } else {
                    resultArea.setText("未找到该朋友！");
                }
                FriendGUI.refreshFriendsList();
            }
        });
    }
}